package com.zhang.first.day07;

import org.apache.flink.api.common.state.ListState;
import org.apache.flink.api.common.state.ListStateDescriptor;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.co.CoProcessFunction;
import org.apache.flink.util.Collector;

/**
 * @title: 实现左外连接  select * from A left join B on A.id = B.id;
 * @author: zhang
 * @date: 2022/1/20 21:27
 */
public class Example2_1 {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        DataStreamSource<Tuple2<String, Integer>> stream1 = env
                .fromElements(
                        Tuple2.of("a", 1),
                        Tuple2.of("b", 1),
                        Tuple2.of("c", 2),
                        Tuple2.of("d", 2),
                        Tuple2.of("e", 2),
                        Tuple2.of("f", 2),
                        Tuple2.of("g", 2)

                );

        DataStreamSource<Tuple2<String, Integer>> stream2 = env
                .fromElements(
                        Tuple2.of("c", 3),
                        Tuple2.of("d", 3)
                );

        stream1.keyBy(r -> r.f0)
                .connect(stream2.keyBy(r -> r.f0))
                .process(new InnerJoin())
                .print();


        env.execute();
    }

    public static class InnerJoin extends CoProcessFunction<Tuple2<String, Integer>, Tuple2<String, Integer>, String> {
        //声明状态
        //保存第一条流的历史数据
        private ListState<Tuple2<String, Integer>> history1;
        //保存第二条流的历史数据
        private ListState<Tuple2<String, Integer>> history2;

        @Override
        public void open(Configuration parameters) throws Exception {
            history1 = getRuntimeContext().getListState(
                    new ListStateDescriptor<Tuple2<String, Integer>>(
                            "history_1",
                            Types.TUPLE(Types.STRING, Types.INT)
                    )
            );

            history2 = getRuntimeContext().getListState(
                    new ListStateDescriptor<Tuple2<String, Integer>>(
                            "history_2",
                            Types.TUPLE(Types.STRING, Types.INT)
                    )
            );
        }

        @Override
        public void processElement1(Tuple2<String, Integer> value, CoProcessFunction<Tuple2<String, Integer>, Tuple2<String, Integer>, String>.Context ctx, Collector<String> out) throws Exception {
            //history1.add(value);
            // 当前数据和第二条流的所有历史数据进行join
            if (history2.get().spliterator().getExactSizeIfKnown() == 0) {
                out.collect(value + "===>"+"(null,null)");
            } else {
                for (Tuple2<String, Integer> e : history2.get()) {
                    out.collect(value + "===>" + e);
                }
            }

        }

        @Override
        public void processElement2(Tuple2<String, Integer> value, CoProcessFunction<Tuple2<String, Integer>, Tuple2<String, Integer>, String>.Context ctx, Collector<String> out) throws Exception {
            history2.add(value);
        }
    }
}
